package com.key.service.impl;

import com.key.common.R;
import com.key.common.dto.*;
import com.key.common.vo.AssetsTVO;
import com.key.entity.*;
import com.key.mapper.*;
import com.key.service.ApprovalService;
import com.key.utils.ExcelExportHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: li
 * @CreateTime: 2023-02-24  19:04
 */
@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    ApprovalMapper approvalMapper;
    @Autowired
    ReceiptAssetsMapper receiptAssetsMapper;
    @Autowired
    BorrowAssetsMapper borrowAssetsMapper;
    @Autowired
    WithdrawalAssetsMapper withdrawalAssetsMapper;
    @Autowired
    ReturnAssetsMapper returnAssetsMapper;



    @Override
    public R showAllPendingApprovalInfo(Approval approval) {
        R r = new R();

        try {
            List<Approval> approvalList = approvalMapper.selectAllPendingApprovalInfo(approval);
            System.out.println(approvalList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(approvalList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R showAllAlreadyApprovalInfo(Approval approval) {
        R r = new R();

        try {
            List<Approval> approvalList = approvalMapper.selectAllAlreadyApprovalInfo(approval);
            System.out.println(approvalList);
            r.setCode(200);
            r.setMsg("success");
            r.setData(approvalList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    @Transactional
    public R approvalYes(Integer[] ids) {

        R r = new R();



        String state="待发放";
        int i=0;
        try {
            for (Integer id:ids){
                //修改审批单状态为"待发放"
                i = approvalMapper.updateApprovalState(id, state);
                Approval approval = approvalMapper.selectOneApprovalInfo(id);
                String number= approval.getApprovalNo();
                if ("领用".equals(approval.getApplicationType())){
                    // TODO: 2023/2/25 调用谭超的mapper，传给他单号
                    ReceiptDTO receiptDTO=new ReceiptDTO();
                    receiptDTO.setReceiptNumber(number);
                    receiptDTO.setState("已审批");
                    receiptAssetsMapper.updateReceiptAssets(receiptDTO);
                }
                else if("借用".equals(approval.getApplicationType())){
                    // TODO: 2023/2/25 调用谭超的mapper，传给他单号
                    BorrowDTO borrowDTO=new BorrowDTO();
                    borrowDTO.setBorrowNumber(number);
                    borrowDTO.setStates("已审批");
                    borrowAssetsMapper.updateBorrowAssets(borrowDTO);
                }
                else if("归还".equals(approval.getApplicationType())){
                    // TODO: 2023/2/25 调用谭超的mapper，传给他单号
                    ReturnDTO returnDTO=new ReturnDTO();
                    returnDTO.setReturnNumber(number);
                    returnDTO.setStates("已审批");
                    returnAssetsMapper.updateReturnAssets(returnDTO);
                }
                else if("退库".equals(approval.getApplicationType())){
                    // TODO: 2023/2/25 调用谭超的mapper，传给他单号
                    WithdrawalDTO withdrawalDTO=new WithdrawalDTO();
                    withdrawalDTO.setWithdrawalNumber(number);
                    withdrawalDTO.setState("已审批");
                    withdrawalAssetsMapper.updateWithdrawalAssets(withdrawalDTO);
                }
            }
            if (i>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
            else {
                r.setCode(500);
                r.setMsg("修改失败");
            }
        } catch (NumberFormatException e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }


        return r;
    }

    @Override
    @Transactional
    public R approvalNo(Integer[] ids) {
        R r = new R();



        String state="未通过";
        int i=0;
        try {
            for (Integer id:ids){
                //修改审批单状态为"未通过"
                i = approvalMapper.updateApprovalState(id, state);
                Approval approval = approvalMapper.selectOneApprovalInfo(id);
                if ("领用".equals(approval.getApplicationType())){
                    List<Map<String, Long>> list = approvalMapper.selectAssetsReceiptByApprovalNum(approval.getApprovalNo());
                    for (Map<String,Long> map : list) {
                        System.out.println(map.get("assets_id"));
                        int updateAssetsStateByApproval = approvalMapper.updateAssetsStateByApproval(map.get("assets_id"));
                    }
                }
                else if("借用".equals(approval.getApplicationType())){
                    List<Map<String, Long>> list = approvalMapper.selectAssetsBorrowByApprovalNum(approval.getApprovalNo());
                    for (Map<String,Long> map : list) {
                        System.out.println(map.get("assets_id"));
                        int updateAssetsStateByApproval = approvalMapper.updateAssetsStateByApproval(map.get("assets_id"));
                    }

                }
                else if("归还".equals(approval.getApplicationType())){
                    List<Map<String, Long>> list = approvalMapper.selectAssetsBorrowByApprovalNum(approval.getApprovalNo());
                    for (Map<String,Long> map : list) {
                        System.out.println(map.get("assets_id"));
                        int updateAssetsStateByApproval = approvalMapper.updateAssetsStateByApproval(map.get("assets_id"));
                    }
                }
                else if("退库".equals(approval.getApplicationType())){
                    List<Map<String, Long>> list = approvalMapper.selectAssetsReceiptByApprovalNum(approval.getApprovalNo());
                    for (Map<String,Long> map : list) {
                        System.out.println(map.get("assets_id"));
                        int updateAssetsStateByApproval = approvalMapper.updateAssetsStateByApproval(map.get("assets_id"));
                    }
                }


            }
            if (i>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
            else {
                r.setCode(500);
                r.setMsg("修改失败");
            }
        } catch (NumberFormatException e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }


        return r;
    }

    @Override
    public R showApprovalByIdAndState(String num, String state) {

        R r=new R();
        try {
            if ("领用".equals(state)){
                ReceiptDTO receiptDTO=new ReceiptDTO();
                receiptDTO.setReceiptNumber(num);
                List<ReceiptAssets> list= receiptAssetsMapper.selectReceipt(receiptDTO);
                List<AssetsTVO> assetsList=receiptAssetsMapper.selectAssetsTVOByReceiptAssetsNumber(num);
                HashMap<String, Object> map = new HashMap<>();
                map.put("领用信息单",list);
                map.put("领用的资产列表",assetsList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(map);

            }
            else if("借用".equals(state)){
                BorrowDTO borrowDTO=new BorrowDTO();
                borrowDTO.setBorrowNumber(num);
                List<BorrowAssets> list= borrowAssetsMapper.selectBorrow(borrowDTO);
                List<Assets> assetsList=borrowAssetsMapper.selectAssetsByBorrowAssetsNumber(num);
                HashMap<String, Object> map = new HashMap<>();
                map.put("借用信息单",list);
                map.put("借用的资产列表",assetsList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(map);
            }
            else if("归还".equals(state)){
                ReturnDTO returnDTO=new ReturnDTO();
                returnDTO.setReturnNumber(num);
                List<ReturnAssets> list=returnAssetsMapper.selectReturn(returnDTO);
                List<Assets> assetsList=returnAssetsMapper.selectAssetsByReturnAssets(num);
                HashMap<String, Object> map = new HashMap<>();
                map.put("归还信息单",list);
                map.put("归还的资产列表",assetsList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(map);
            }
            else if("退库".equals(state)){
                WithdrawalDTO withdrawalDTO=new WithdrawalDTO();
                withdrawalDTO.setWithdrawalNumber(num);
                List<WithdrawalAssets> list= withdrawalAssetsMapper.selectWithdrawal(withdrawalDTO);
                List<Assets> assetsList=withdrawalAssetsMapper.selectAssetsByWithdrawalAssetsNumber(num);
                HashMap<String, Object> map = new HashMap<>();
                map.put("退库信息单",list);
                map.put("退库的资产列表",assetsList);
                r.setCode(200);
                r.setMsg("success");
                r.setData(map);
            }
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R showEmpBorrowInfo(Approval approval) {
        R r = new R();

        try {
            List<Approval> approvalList = approvalMapper.selectEmpBorrowInfo(approval);
            r.setCode(200);
            r.setMsg("success");
            r.setData(approvalList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R showEmpCollectingInfo(Approval approval) {
        R r = new R();

        try {
            List<Approval> approvalList = approvalMapper.selectEmpCollectingInfo(approval);
            r.setCode(200);
            r.setMsg("success");
            r.setData(approvalList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R assetsProvideEmp(Integer[] ids) {
        R r = new R();
       // String[] split = ids.split(",");


        String state="待签收";
        int i=0;
        try {
            for (Integer id:ids){
                //修改审批单状态为"待签收"
                i = approvalMapper.updateApprovalState(id, state);
                Approval approval = approvalMapper.selectOneApprovalInfo(id);
            }
            if (i>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
            else {
                r.setCode(500);
                r.setMsg("修改失败");
            }
        } catch (NumberFormatException e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }


        return r;
    }

    @Override
    public HSSFWorkbook getWork() {
        String[] header = {"主键","审批状态","审批单号","审批类型","申请人","申请公司","申请部门","申请时间"};
        String[] properties = {"id","state","approvalNo","applicationType","applicationPerson","applicationCompany","applicationDepartment","applicationTime"};
        Approval approval = new Approval();
        List list = new ArrayList();
        List<Approval> approvalList = approvalMapper.selectEmpBorrowInfo(approval);
        for (Approval mainInfoSingle : approvalList) {
            list.add(mainInfoSingle);
        }
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelExportHelper.exportExcel(header, properties, list, "员工申请单");
        return workbook;
    }
    @Override
    public HSSFWorkbook getWork2() {
        String[] header = {"主键","审批状态","审批单号","审批类型","申请人","申请公司","申请部门","申请时间"};
        String[] properties = {"id","state","approvalNo","applicationType","applicationPerson","applicationCompany","applicationDepartment","applicationTime"};
        Approval approval = new Approval();
        List list = new ArrayList();
        List<Approval> approvalList = approvalMapper.selectEmpCollectingInfo(approval);
        for (Approval mainInfoSingle : approvalList) {
            list.add(mainInfoSingle);
        }
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        HSSFWorkbook workbook = excelExportHelper.exportExcel(header, properties, list, "员工申请单");
        return workbook;
    }

}
